<?php
$resourceStr = Yii::t('site','Resource');
$deleteStr = Yii::t('site','Delete');
$modifyStr = Yii::t('site','Modify');
$addResourceStr = Yii::t('site','Add');
$timeslotStr = Yii::t('site','TimeSlot');
$creatorStr = Yii::t('site','Creator');
$resourceNameStr = Yii::t('site','Name');
$statusStr = Yii::t('site','Status');
$actionStr = Yii::t('site','Action');
$amountStr = Yii::t('site','Amount');
$prevStr = Yii::t('site','<<');
$nextStr = Yii::t('site','>>');
$activeStr = Yii::t('site','active');

$screen = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen();
$app = Blyn::app()->getActiveApp();
$timeslots = Blyn::app()->getActiveApp()->getTimeslots(null);
$timeslotList = array();
$timeslotInfoList = array();
foreach ($timeslots as $id=>$item) {        
    $timeslotList[$id] = $item->name;
    
    $pieces = explode(" ", $item->cronValue);

    $startEndHours = explode("-", $pieces[1]);
    $startHour = $startEndHours[0];
    $endHour = $startEndHours[1];

    $repeatWeekDay = $pieces[4];
    
    $timeslotInfoList[$id] = array('startHour'=>$startHour, 'endHour'=>$endHour, 'repeatWeekDay'=>$repeatWeekDay);
}

if ($screen->isAjaxLinkClicked($deleteStr)) {
    if ($app instanceof BBookingSports) {
        if ($app instanceof BBookingSports) {
        if (!empty($_POST))
        { 
            $checkedArray = $_POST['checkbox']; 
        } 
        foreach ($checkedArray as $primary_id){
            $resourceModel = new BSBookableResourceFormModel();                    
            $resourceModel->id = $primary_id;
//                    $resourceModel->name = $resource->name;
//                    $resourceModel->creator = $resource->creator;
            $app->deleteBookableResource($resourceModel);
        }        
       }
    }
}

$resources = Blyn::app()->getActiveApp()->getBookableResourceList();

$totalRows = count($resources);
$rowsPerPage = 5;
if (!empty($_POST))
    $pageNo = $_POST['curPageNo'];
else
    $pageNo = 1;
$totalpages = ceil($totalRows/$rowsPerPage);

if ($screen->isAjaxLinkClicked($prevStr))
{
        if ($app instanceof BBookingSports) {
            if (!empty($_POST))
            { 
                $pageNo--;
                if($pageNo<1)
                    $pageNo = 1;               
            }
        }
}

if ($screen->isAjaxLinkClicked($nextStr))
{
        if ($app instanceof BBookingSports) {
            if (!empty($_POST))
            { 
                $pageNo++;
                if($pageNo>$totalpages)
                    $pageNo = $totalpages;               
            }
        }
}
    
//$p2btn = $screen->buildAjaxLink('p2', '2', NULL, 'resourceList', array('update' => "#resourceDataTable", 'type' => 'post'), array('class' => 'btn'));
for($j=0; $j<$totalpages; $j++)
{
    $page_number = strval($j + 1);
    if ($screen->isAjaxLinkClicked($page_number))
    {
        if ($app instanceof BBookingSports) {
            if (!empty($_POST))
            { 
                $pageNo = $page_number;
                break;
            }
        }
    }
}

$startNo = ($pageNo-1)*$rowsPerPage;
if($totalRows < $pageNo*$rowsPerPage)
    $endNo = $totalRows;
else
    $endNo = $startNo + $rowsPerPage;
?>


<script type="text/javascript"> 
  $(document).ready(function(){ 
    $("#selectAll").click(function(){ 
      if($(this).attr("checked")=="checked"){ 
        $(":checkbox").attr("checked",true); 
      }else{ 
        $(":checkbox").attr("checked",false); 
      } 
    }); 
    
  }); 
 </script> 
 
<div><legend><?php echo Yii::t('site','Bookable Resource List'); ?></legend></div>
<?php echo CHtml::beginForm(); ?>
<fieldset>
    <table class="table table-hover">
        <thead>
            <tr>
                <th>
                    <input type="checkbox"  id="selectAll"> 
                </th>
                <th><?php echo $timeslotStr; ?></th>
                <th><?php echo $resourceStr; ?></th>
                <th><?php echo $amountStr; ?></th>
                <th><?php echo $statusStr; ?></th>
                <th><?php echo $actionStr; ?></th>
            </tr> 
        </thead>
        <tbody>
            <?php
            $i = 0;
            foreach ($resources as $resource) {
                 if($i>=$startNo && $i<=$endNo-1)
                 {
                if ($resource instanceof BSBookableResource) {
                    $primaryId = $resource->getId();
                    $resourceId = $resource->resourceId;
                    $creatorName = 'no-name';
                    $creatorId = $resource->creatorId;
                    $amount = $resource->amount;
                    if (is_numeric($creatorId) && $creatorId > 0) {
                        $creator = new BUser($creatorId);
                        $creatorName = $creator->getName();
                    }
                    $statusName = $activeStr;
                    if (is_numeric($resource->status) && $resource->status > 0) {
                        $status = new BSStatus($resource->status);
                        $statusName = $status->getName();
                    }
                    if (is_numeric($resourceId) && $resourceId > 0) {
                        $r = new BSResource($resourceId);
                        $resourceName = $r->getName();
                    }
                    $timeslotId = $resource->timeslotId;
                    
                    if($timeslotId>0)
                    {
                        $timeslotRow = new BSTimeslot($timeslotId);
                        $timeslotName = $timeslotRow->name;
                    }
                    else
                        $timeslotName = '';
                }
                //   $ajaxJoinBtn = $screen->buildAjaxLink('joinSpaceBtn', 'Join to Space', 'leftMenu', NULL, array('update' => "#leftbar", 'type' => 'post'), array('id' => 'joinBtn' . $space->getId(), 'class' => 'btn'));
                //$spaceId = $space->getId();
                //$senderPermits = $screen->getSenderPermits($spaceId);
                //$allowRoles = current($senderPermits);
                ?>
                <tr>
                    <td><label class="checkbox">
                            <input type="checkbox" name="checkbox[]" value=<?php echo $primaryId?>> 
                        </label></td>
                    <td>
                        <?php echo $timeslotName; ?>
                    </td>
                    <td>
                        <?php echo $resourceName; ?>
                    </td>
                    <td>
                        <?php echo $amount; ?>
                    </td>
                    <td>
                        <?php echo $statusName; ?>
                    </td>
                    <td>
                        <div class="row-fluid">
                            <a href="#myModal<?php echo $primaryId; ?>" role="button" data-toggle="modal"><?php echo $modifyStr; ?></a>
                        </div>
                    </td>
                </tr>
                <!-- Modal -->
            <div id="myModal<?php echo $primaryId; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <?php $screen->renderView('manageBookable', array('bResourceId' => $primaryId, 'bResource' => $resources[$primaryId], 'timeslotList'=>$timeslotList, 'timeslotInfoList'=>$timeslotInfoList)); ?>
            </div>

        <?php }
            $i++;
            }?>

        </tbody>
    </table>
    <div class="row-fluid">
        <a href="#myModal0" role="button" data-toggle="modal" class="btn"><?php echo $addResourceStr; ?></a>
        <?php if($totalpages>0){ ?>
        <?php echo $screen->getAjaxLink($deleteStr, array('htmlOptions'=>array('class'=>'btn'))); ?>
        <?php } ?>
    </div>
    <div id="myModal0" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <?php $screen->renderView('manageBookable', array('bResourceId' => '0', 'bResource' => null, 'timeslotList'=>$timeslotList, 'timeslotInfoList'=>$timeslotInfoList)); ?>
    </div>
    <div><input type="hidden" name="curPageNo" value=<?php echo $pageNo; ?>></div>
    <?php echo $screen->getAjaxLink('Refresh', array('htmlOptions'=>array('id'=>'refreshIt', 'style'=>'display:none'))); ?>
</fieldset>

<body>  
<div class="pagination">  
  <ul>  
    <li <?php
        $disabled = ($pageNo==1 || $totalpages==0);
        if ($disabled) echo "class='active'"; ?>>
            <?php 
            if($pageNo + 1>$totalpages)
                $curPageNoStr = strval($totalpages);
            else
                $curPageNoStr = strval($pageNo + 1);
            
            if ($disabled) {
                echo '<a href="javascript:void()">'.$prevStr.'</a>';
            } else {
                echo $screen->getAjaxLink($prevStr, array('data'=>$curPageNoStr));
            }
            ?>
    </li>  
    <?php for($j=0; $j<$totalpages; $j++){ 
        $curPageNoStr = strval($j + 1);
    ?>
    <li <?php
        $disabled = ($pageNo == $j + 1);
        if ($disabled) echo "class='active'"; ?>>
        <?php
            if ($disabled) {
                echo '<a href="javascript:void()">'.$curPageNoStr.'</a>';
            } else {
                echo $screen->getAjaxLink($curPageNoStr);
            }
        ?>
    </li>  
    <?php }
    ?>
    <li <?php
        $disabled = ($pageNo==$totalpages || $totalpages==0);
        if ($disabled) echo "class='active'"; ?>>
            <?php 
            if($pageNo<2)
                $curPageNoStr = '1';
            else
                $curPageNoStr = strval($pageNo - 1);
            
            if ($disabled) {
                echo '<a href="javascript:void()">'.$nextStr.'</a>';
            } else {
                echo $screen->getAjaxLink($nextStr, array('data'=>$curPageNoStr));
            }
            ?>
    </li>  
  </ul>  
</div>  
</body>
<?php echo CHtml::endForm(); ?>



